// 获取验证码
function getCode() {
    const chars =
        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var captchaCode = "";
    for (let i = 0; i < 4; i++) {
        captchaCode += chars.charAt(
            Math.floor(Math.random() * chars.length)
        );
    }
    return captchaCode
}

// 绘制验证码
export function drawCode(elementId) {
    const captchaCode = getCode();
    const canvas = document.getElementById(elementId);
    const ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.font = "24px Arial";
    ctx.fillStyle = "#000";
    ctx.fillText(captchaCode, 10, 30);
    // Adding noise
    for (let i = 0; i < 5; i++) {
        ctx.strokeStyle =
            "#" + Math.floor(Math.random() * 0xffffff).toString(16);
        ctx.beginPath();
        ctx.moveTo(
            Math.floor(Math.random() * canvas.width),
            Math.floor(Math.random() * canvas.height)
        );
        ctx.lineTo(
            Math.floor(Math.random() * canvas.width),
            Math.floor(Math.random() * canvas.height)
        );
        ctx.stroke();
    }
}